package com.sys.gradepaysys.controller;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sys.gradepaysys.model.*;
import com.sys.gradepaysys.service.EventService;
import com.sys.gradepaysys.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 学生控制器
 *
 * @version V1.0
 * @Package com.asiainfo.core.*.*.*
 */
@Slf4j
@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    EventService eventService;
    @Autowired
    StudentService studentService;

    @RequestMapping("/selectEventIsDoing")
    @ResponseBody
    public ResultData selectEventIsDoing() {
        List<Event> eventListIsDoing = eventService.getEventListIsDoing();
        log.info(eventListIsDoing.toString());

        return new ResultData<>(200, null, eventListIsDoing);

    }

    @RequestMapping("/getPayEvents")
    @ResponseBody
    public ResultData getPayEvents() {
        List<Event> eventListPay = eventService.getEventListPay();
        log.info(eventListPay.toString());

        return new ResultData<>(200, null, eventListPay);

    }

    @RequestMapping("/getStuOneList")
    @ResponseBody
    public ResultData getStuOneList(HttpServletRequest request) {
        Student stu = (Student) request.getSession().getAttribute("user");
        log.info(stu.toString());
        List<Map<String, String>> stuOneList = studentService.getStuOneList(String.valueOf(stu.getsId()));

        return new ResultData<>(200, null, stuOneList);
    }

    @RequestMapping("/getIncomeList")
    @ResponseBody
    public ResultData getIncomeList() {
        List<Map<String, String>> incomeList = eventService.getIncomeList();
        log.info(incomeList.toString());

        return new ResultData<>(200, null, incomeList);
    }

    @RequestMapping("/getOutEventList")
    @ResponseBody
    public ResultData getOutEventList() {
        List<Map<String, String>> incomeList = eventService.getOutEventList();
        log.info(incomeList.toString());

        return new ResultData<>(200, null, incomeList);
    }

    @RequestMapping("/updatePwd")
    @ResponseBody
    public ResultData updatePwd(HttpServletRequest request,String newpwd) {
        Student stu = (Student) request.getSession().getAttribute("user");
        log.info(stu.toString());
        boolean b = studentService.updatePwd(newpwd, String.valueOf(stu.getsId()));
        ResultData resultData = null;
        if (b){
            resultData=new ResultData<>(200, "修改成功!", null);
        }else {
            resultData=new ResultData<>(200, "修改失败!", null);
        }
        return resultData;
    }

    @RequestMapping("/registerAdd")
    @ResponseBody
    public ResultData registerAdd(HttpServletRequest request) {
        String sNum = request.getParameter("sNum");
        String name = request.getParameter("sName");
        String password = request.getParameter("password");
        String grade = request.getParameter("grade");
        String clazz = request.getParameter("clazz");
        String tel = request.getParameter("tel");
        String sex = request.getParameter("sex");
        String qq = request.getParameter("qq");
        Student student = new Student();
        student.setsNum(sNum);
        student.setPassword(password);
        student.setsName(name);
        student.setgName(grade);
        student.setCname(clazz);
        student.setsTel(tel);
        student.setSex(sex);
        student.setQq(qq);
        log.info(student.toString());
        boolean b = studentService.registerStu(student);
        ResultData resultData = null;
        if (b) {
            resultData = new ResultData<>(200, "注册成功!", null);
        } else {
            resultData = new ResultData<>(200, "注册失败!", null);
        }

        return resultData;
    }
}
